package me.pplay.playtalk.database;

import java.util.ArrayList;
import java.util.List;

import me.pplay.playtalk.bean.Chat;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.util.LogManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class ChatListDb {
	private final String CHATLIST_TBNAME	= "chatlist";
	private DbHelper dbHelper;
	public ChatListDb(){}
	
	public ChatListDb(Context context){
		dbHelper	= new DbHelper(context);
	}

	/**
	 * 判断
	 * @return
	 */
	public boolean isExists(int skyid){
		Cursor cursor = null;
		try {
			dbHelper.openDatabase();
			cursor = dbHelper.db.query(CHATLIST_TBNAME, null, "myskyid=" + Globals.skyid + " and skyid = " + skyid, null, null,null, null);
			if (cursor.getCount() > 0) {
				return true;
			}
		} catch (Exception e) {
		} finally {
			if(cursor != null)
				cursor.close();
			dbHelper.closeDatabase();
		}
		return false;
	}
	
	/**
	 * 添加
	 * @return
	 */
	public boolean add(Chat chat){
		dbHelper.openDatabase();
		boolean bool = false;
		long result = 0;
		try{
			ContentValues values = new ContentValues();
			values.put("myskyid", chat.getMyskyid());
			values.put("type", chat.getType());
			values.put("skyid", chat.getSkyid());
			values.put("name", chat.getName());
			values.put("time", chat.getTime());
			values.put("count", chat.getCount());
			result = dbHelper.db.insert(CHATLIST_TBNAME, null, values);
		}catch (Exception e) {
			LogManager.LogShow("ChatListDb", e.getMessage());
		}finally{
			dbHelper.closeDatabase();
			if(result >0)
				bool = true;
		}
		return bool;
	}
	
	
	/**
	 * 更新数据
	 */
	public void update(Chat chat) {
		dbHelper.openDatabase();
		ContentValues values;
		try {
			if (chat != null) {
				values = new ContentValues();
				values.put("_id", chat.get_id());
				values.put("myskyid", chat.getMyskyid());
				values.put("type", chat.getType());
				values.put("skyid", chat.getSkyid());
				values.put("name", chat.getName());
				values.put("time", chat.getTime());
				values.put("count", chat.getCount());
				dbHelper.db.update(CHATLIST_TBNAME, values, "_id=" + chat.get_id(), null);
			}
		} catch (Exception e) {
		} finally {
			dbHelper.closeDatabase();
		}
	}
	
	/**
	 * 查询
	 */
	public Chat query(int skyid){
		dbHelper.openDatabase();
		Chat chat				= null;
		Cursor cursor 			= null;
		try {
			String sql			= "select * from " + CHATLIST_TBNAME + " where myskyid="+Globals.skyid +" and skyid="+skyid;
			cursor 				= dbHelper.db.rawQuery(sql, null);
			if(cursor == null)
				return null;
			if (cursor.moveToNext()) {
				chat	= new Chat();
				chat.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
				chat.setMyskyid(Globals.skyid);
				chat.setType(cursor.getInt(cursor.getColumnIndex("type")));
				chat.setSkyid(skyid);
				chat.setName(cursor.getString(cursor.getColumnIndex("name")));
				chat.setTime(cursor.getString(cursor.getColumnIndex("time")));
				chat.setCount(cursor.getInt(cursor.getColumnIndex("count")));
            }
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			dbHelper.closeDatabase();
		}
		return chat;
	}
	
	public List<Chat> queryList(int type){
		dbHelper.openDatabase();
		List<Chat> chatList		= new ArrayList<Chat>();
		Cursor cursor 			= null;
		try {
			String sql			= "select * from " + CHATLIST_TBNAME + " where myskyid="+Globals.skyid +" and type ="+ type +"  order by time desc";
			cursor 				= dbHelper.db.rawQuery(sql, null);
			if(cursor != null){
				while (cursor.moveToNext()) {
					Chat chat	= new Chat();
					chat.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
					chat.setMyskyid(Globals.skyid);
					chat.setType(type);
					chat.setSkyid(cursor.getInt(cursor.getColumnIndex("skyid")));
					chat.setName(cursor.getString(cursor.getColumnIndex("name")));
					chat.setTime(cursor.getString(cursor.getColumnIndex("time")));
					chat.setCount(cursor.getInt(cursor.getColumnIndex("count")));
					chatList.add(chat);
	            }
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			dbHelper.closeDatabase();
		}
		return chatList;
	}
	
	/**
	 * 删除
	 */
	public void delete(int _id) {
		dbHelper.openDatabase();
		dbHelper.db.delete(CHATLIST_TBNAME, "_id=" + _id + "", null);
		dbHelper.closeDatabase();
	}
}
